<template>
  <div class="mes-people">
    <div class="mes-people_top" @click="open">
      <div>好友通知</div>
      <div class="icon" v-if="appunread > 0">{{ appunread }}</div>
      <el-icon><ArrowRight /></el-icon>
    </div>
    <div class="mes-people-tab">
      <div
        @click="changeTab('friend')"
        :class="activename == 'friend' ? 'active' : ''"
        class="mes-people-tab-item"
      >
        好友
      </div>
      <div
        @click="changeTab('group')"
        :class="activename == 'group' ? 'active' : ''"
        class="mes-people-tab-item"
      >
        群聊
      </div>
    </div>
    <div class="mes-people-content">
      <div v-show="activename == 'friend'">
        <div
          class="mes-people-content-item"
          v-for="item in groupList"
          :key="item.group_id"
        >
          <div
            style="
              display: flex;
              align-items: center;
              padding: 0 10% 0;
              cursor: pointer;
            "
            @click="expanded(item)"
          >
            <el-icon
              style="margin-right: 5px"
              :class="isexpand(item.group_id) ? 'expand' : 'unexpand'"
              ><ArrowRight
            /></el-icon>
            <div class="title">{{ item.group_name }}</div>
            <div class="num">{{ item.friendNum }}</div>
          </div>
          <div style="margin-top: 10px" v-show="isexpand(item.group_id)">
            <friend
              v-for="_item in item.friendlist"
              :key="_item.friend_id"
              :dataObj="_item"
              @changefriend="changefriend"
              @dblclick="sendmessage(_item)"
            ></friend>
          </div>
        </div>
      </div>
      <div v-show="activename == 'group'">22</div>
    </div>
  </div>
</template>

<script>
import friend from './friend.vue'
import { changeread, checkApplication } from '@/api/message'
export default {
  props: {
    groupList: {
      type: Array,
      default: () => { }
    }
  },
  components: {
    friend
  },
  watch: {
    '$store.state.appunread'(value) {
      console.log(111);
      this.appunread = localStorage.getItem('appunread')
    }
  },
  computed: {
    // isexpand(id) {
    //   return this.expandList.includes(id)
    // }
  },
  data() {
    return {
      activename: 'friend',
      expandList: [],
      appunread: 0
    }
  },
  methods: {
    changeTab(val) {
      this.activename = val
    },
    expanded(data) {
      if (this.expandList.includes(data.group_id)) {
        this.expandList = this.expandList.filter(item => item != data.group_id)
      } else {
        this.expandList.push(data.group_id)
        this.$emit('expanded', {
          data: data,
        })
      }
    },
    isexpand(id) {
      return this.expandList.includes(id)
    },
    clearexpandList() {
      this.expandList = []
    },
    changefriend(data) {
      this.$emit('changefriend', data)
    },
    sendmessage(val) {
      console.log(111);
      this.$emit('sendmessage', val)
    },
    // 打开好友申请
    open() {
      changeread({ UserId: localStorage.getItem('userId') })
      checkApplication({ UserId: localStorage.getItem('userId'), state: 1 })
      this.$emit('open')
    }
  },
}
</script>

<style lang="scss" scoped>
.mes-people {
  .active {
    color: #2b2bb3;
    background: #fff;
  }
  .expand {
    transform: rotate(90deg);
    transition: all 0.3s;
  }
  .unexpand {
    transform: rotate(0deg);
    transition: all 0.3s;
  }
  .mes-people-tab {
    width: 80%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px auto 0;
    background: #f5f5f5;
    border-bottom: 1px solid #e5e5e5;
    padding: 5px;
    border-radius: 5px;
    .mes-people-tab-item {
      width: 50%;
      height: 80%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;
      font-weight: 500;
      color: #999;
      cursor: pointer;
    }
  }
  .mes-people-content {
    padding: 10px 0 0 0;
    .mes-people-content-item {
      display: flex;
      flex-direction: column;
      margin-bottom: 15px;
      .title {
        flex: 1;
      }
      .num {
        width: 20px;
        font-size: 12px;
        color: #d0c7c7;
      }
    }
  }
  .mes-people_top {
    width: 80%;
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px auto 0;
    border-bottom: 1px solid #e5e5e5;
    padding: 5px;
    border-radius: 5px;
    font-size: 14px;
    cursor: pointer;
    position: relative;
  }
  .mes-people_top:hover {
    background: #f5f5f5;
  }
  .icon {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: red;
    position: absolute;
    top: 50%;
    right: 30px;
    transform: translateY(-50%);
    text-align: center;
    line-height: 15px;
    color: #fff;
    font-size: 12px;
  }
}
</style>